package com.haoma.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.haoma.dto.ArticleDto;
import com.haoma.dto.LabelDto;
import com.haoma.elasticsearch.ArticleEs;
import com.haoma.result.Result;
import com.haoma.service.ArticleService;
import com.haoma.service.CategoryService;
import com.haoma.service.LabelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * Description: 文章相关接口
 */
@RestController
@RequestMapping("/admin/article")
public class ArticleController {

    // 文章相关接口
    @Autowired
    private ArticleService articleService;



    /**
     * 图片上传
     * @param multipartFile
     * @return
     */
    @PostMapping("/article/uploadImage")
    public Result uploadImage(@RequestParam("file") MultipartFile multipartFile){
        String fileUrl = articleService.uploadImage(multipartFile);
        return Result.success(fileUrl);
    }

    /**
     * 修改文章状态
     *
     * @param id
     * @param status
     * @return
     */
    @PutMapping("/article/{id}/{status}")
    public Result updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        articleService.updateStatus(id, status);
        return Result.success();
    }

    /**
     * 根据文章id获取文章详情
     * @param id
     * @return
     */
    @GetMapping("/article/{id}")
    public Result getArticle(@PathVariable Long id){
        ArticleEs articleEs = articleService.getArticle(id);
        return Result.success(articleEs);
    }

    /**
     * 修改文章
     *
     * @param articleEs
     * @return
     */
    @PutMapping("/article/update/{id}")
    public Result updateArticle(@PathVariable Long id, @RequestBody ArticleEs articleEs) {
        articleService.updateArticle(id, articleEs);
        return Result.success();
    }

    /**
     * 删除文章
     *
     * @param id
     * @return
     */
    @DeleteMapping("/article/del/{id}")
    public Result delArticle(@PathVariable Long id) {
        articleService.delArticle(id);
        return Result.success();
    }

    /**
     * 查询所有的文章
     *
     * @return
     */
    @PostMapping("/article")
    public Result getArticleList(@RequestBody ArticleDto articleDto) {
        List<ArticleEs> articleEsList = articleService.getArticleList(articleDto);
        return Result.success(articleEsList);
    }

    /**
     * 发布文章信息
     *
     * @param articleEs
     * @return
     */
    @PostMapping("/article/save")
    public Result saveArticle(@RequestBody ArticleEs articleEs) {
        articleService.saveArticle(articleEs);
        return Result.success();
    }

    /**
     * 上传文章封面
     *
     * @param multipartFile
     * @return
     */
    @PostMapping("/fileUpload")
    public Result fileUpload(@RequestParam("file") MultipartFile multipartFile) {
        String fileUrl = articleService.fileUpload(multipartFile);
        return Result.success(fileUrl);
    }

    /**
     * 获取分类和标签的信息
     *
     * @return
     */
    @GetMapping("/article/basic")
    public Result getLabelAndCategory() {
        Map<String, List<String>> listMap = articleService.getLabelAndCategory();
        return Result.success(listMap);
    }

    // 分类相关接口
    @Autowired
    private CategoryService categoryService;

    /**
     * 分类的分页查询
     *
     * @param page
     * @param limit
     * @param name
     * @return
     */
    @GetMapping("/category/{page}/{limit}")
    public Result pageCategoryList(@PathVariable Long page, @PathVariable Long limit,
                                   @RequestParam(value = "name", required = false) String name) {
        IPage iPage = categoryService.pageCategoryList(page, limit, name);
        return Result.success(iPage);
    }

    /**
     * 删除分类
     *
     * @param id
     * @return
     */
    @DeleteMapping("/category/{id}")
    public Result delCategory(@PathVariable Integer id) {
        categoryService.delCategory(id);
        return Result.success();
    }

    /**
     * 修改分类
     *
     * @param name
     * @return
     */
    @PutMapping("/category/{id}")
    public Result updateCategory(@PathVariable Integer id, String name) {
        categoryService.updateCategory(id, name);
        return Result.success();
    }

    /**
     * 保存分类
     *
     * @param name
     * @return
     */
    @PostMapping("/category")
    public Result saveCategory(String name) {
        categoryService.saveCategory(name);
        return Result.success();
    }

    // 标签相关接口
    @Autowired
    private LabelService labelService;

    /**
     * 标签列表分页查询
     *
     * @return
     */
    @GetMapping("/label/{page}/{limit}")
    public Result pageLabelList(@PathVariable Long page, @PathVariable Long limit,
                                @RequestParam(value = "name", required = false) String name) {
        IPage iPage = labelService.pageLabelList(page, limit, name);
        return Result.success(iPage);
    }


    /**
     * 新增标签
     *
     * @param labelName
     * @return
     */
    @PostMapping("/label")
    public Result addLabel(@RequestParam String labelName,@RequestParam String color) {
        return labelService.addLabel(labelName,color);
    }

    /**
     * 获取标签
     *
     * @param id
     * @return
     */
    @GetMapping("/label/{id}")
    public Result getLabel(@PathVariable Integer id) {
        return labelService.getLabel(id);
    }

    /**
     * 修改标签
     *
     * @param id
     * @param labelName
     * @return
     */
    @PutMapping("/label/{id}")
    public Result updateLabel(@PathVariable Integer id, @RequestParam String labelName,@RequestParam String color) {
        return labelService.update(id, labelName,color);
    }

    /**
     * 删除标签
     *
     * @param id
     * @return
     */
    @DeleteMapping("/label/{id}")
    public Result deleteLabel(@PathVariable Integer id) {
        return labelService.delete(id);
    }

    /**
     * 批量删除
     *
     * @param labelDto
     * @return
     */
    @DeleteMapping("/label/batch")
    public Result batchDelLabel(@RequestBody LabelDto labelDto) {
        return labelService.batchDelLabel(labelDto);
    }
}
